package com.ls.blog.controller;

import com.ls.blog.dto.*;
import com.ls.blog.service.ConfigService;
import com.ls.blog.service.UserService;
import com.ls.blog.utils.MailUtils;
import com.ls.blog.utils.R;
import com.ls.blog.utils.RedisUtil;
import com.ls.blog.utils.Result;
import com.ls.blog.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.ls.blog.common.consts.Methods.GET;
import static com.ls.blog.common.consts.Methods.POST;
import static com.ls.blog.common.consts.RedisKeyConst.EMAIL_CODE;
import static com.ls.blog.config.RabbitConfig.DELAY_EXCHANGE;
import static com.ls.blog.config.RabbitConfig.DELAY_QUEUE;

@RestController
@Api(tags = "前台首页模块")
@Slf4j
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisUtil redisUtil;




    @GetMapping("/test")
    public void test(){
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE,"YC","hello");
        log.info("当前时间{}",new Date().toString());
    }




    @GetMapping("/")
    @ApiOperation(value = "关于作者",httpMethod = GET)
    public Result<AboutDTO> about(){
        return R.ok(userService.about());
    }

    @GetMapping("/admin")
    @ApiOperation(value = "用户分布",httpMethod = GET)
    public Result<Map<String,Integer>> userMap(){
        return R.ok(userService.userMap());
    }


    @GetMapping("/pub")
    @ApiOperation(value = "首页设置",httpMethod = GET)
    public Result<String> pub(){
        return R.ok(userService.pub());
    }


    @PostMapping("/setting-pub")
    @ApiOperation(value = "首页设置",httpMethod = POST)
    public Result<?> setPub(@RequestBody SettingDTO settingDTO) throws UnsupportedEncodingException {
        userService.setPub(settingDTO.getPub());
        return R.ok();
    }

    @GetMapping("/siteInfo")
    @ApiOperation(value = "站点信息",httpMethod = GET)
    public Result<SiteDTO> siteInfo(){
        return R.ok(userService.siteInfo());
    }


    @GetMapping("/homeConfig")
    @ApiOperation(value = "首页配置信息",httpMethod = GET)
    public Result<?> homeConfig(){
        configService.homeConfig();
        return R.ok(configService.homeConfig());
    }


    @PostMapping("/SetHomeConfig")
    @ApiOperation(value = "首页配置",httpMethod = POST)
    public Result<HomeConfigDTO> setHomeConfig(){
        return R.ok();
    }

    @GetMapping("/social")
    @ApiOperation(value = "社交信息",httpMethod = POST)
    public Result<List<SocialDTO>> social(){
        return R.ok(configService.social());
    }

    @GetMapping("/sendEmail")
    @ApiOperation(value = "发送邮件",httpMethod = GET)
    public Result<?> sendEmail(String email){
        String validCode = userService.getValidCode(new UserVO(email));
        redisUtil.setnx(EMAIL_CODE+email,validCode,60*5);
        return R.ok();
    }

}
